package com.wjq.pay.config;

public class RaceThread{

    public static void main(String[] args) {
        new Thread(new RaceThreads(),"兔子").start();
        new Thread(new RaceThreads(),"乌龟").start();
    }


    public static class RaceThreads extends Thread{
        //赛道总长度
        private int length = 100;

        //计数器
        private  int count = 0;

        //定义一个布尔变量   表示比赛结束
        private static volatile boolean flag = true;
        @Override
        public void run() {

            while (flag){
                /**
                 * 线程休眠200毫秒
                 */
                try {
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                if(Thread.currentThread().getName().equals("乌龟")){

                    count++;

                    System.out.println("乌龟跑了"+count+"米");


                    if(count==length){
                        //比赛结束
                        flag =false;
                        System.out.println("乌龟赢了");
                        break;
                    }
                }


                /**
                 * 线程休眠200毫秒
                 */
                try {
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                if(Thread.currentThread().getName().equals("兔子")){

                    count+=2;

                    System.out.println("兔子跑了"+count+"米");

                    if(count>60){
                        System.out.println("兔子觉得自己行 开始睡觉");
                        try {
                            Thread.sleep(60*1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }

                    if(count==length){
                        //比赛结束
                        flag =false;
                        System.out.println("兔子赢了");
                        break;
                    }
                }

            }



        }
    }
}
